package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.MaterialToleranceTypeEntity;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.repository.master.MaterialToleranceTypeRepository;
import cn.com.huzo.service.master.MaterialToleranceTypeManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;

/**
 * 允差类型管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("MaterialToleranceTypeManager")
public class MaterialToleranceTypeManagerImpl extends GenericManagerImpl<MaterialToleranceTypeEntity, Long>
        implements MaterialToleranceTypeManager {

    @Resource(name = "MaterialToleranceTypeRepository")
    private MaterialToleranceTypeRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) ((Integer) criterion.get("Limit")) * (((Integer) criterion.get("PageIndex")) - 1);
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<MaterialToleranceTypeEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<MaterialToleranceTypeEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<MaterialToleranceTypeEntity> fetchAll() throws Exception {
        return _dao.fetchAll();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public MaterialToleranceTypeEntity fetchByTypeNo(String typeNo) throws Exception {
        return _dao.fetchByTypeNo(typeNo);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional//(isolation = Isolation.READ_COMMITTED)
    @Override
    public Long insert(MaterialToleranceTypeEntity instance) throws Exception {
        Long key = _dao.insert(instance);
        // 审计日志
        if (key > 0) {
            writeAuditLog(instance.getUpdatedBy(), "基础信息→允差类型", "允差类型：" + instance.getTypeName(), "增加");
        }

        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(Map<String, Object> criterion) throws Exception {
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;

        ObjectMapper mapper = new ObjectMapper();
        List<MaterialToleranceTypeEntity> insertList = mapper.convertValue(MapUtils.getObject(criterion, "insertList"), new TypeReference<List<MaterialToleranceTypeEntity>>() {
        });
        List<MaterialToleranceTypeEntity> updateList = mapper.convertValue(MapUtils.getObject(criterion, "updateList"), new TypeReference<List<MaterialToleranceTypeEntity>>() {
        });
        Map<String, Object> _pageData = (Map<String, Object>) criterion.get("pageData");

        if (!insertList.isEmpty()) {
            _rowsAffected1 = _dao.batchInsert(insertList);
        }
        if (!updateList.isEmpty()) {
            _rowsAffected2 = _dao.batchUpdate(updateList);
        }

        if (_rowsAffected1 > 0) {
            // 审计日志
            for (int i = 0; i < insertList.size(); i++) {
                MaterialToleranceTypeEntity instance = insertList.get(i);
                writeAuditLog(insertList.get(0).getUpdatedBy(), "基础信息→允差类型", "允差类型：" + instance.getTypeName(), "增加");
            }
        }
        if (_rowsAffected2 > 0) {
            // 审计日志
            for (int i = 0; i < updateList.size(); i++) {
                MaterialToleranceTypeEntity instance = updateList.get(i);
                writeAuditLog(updateList.get(0).getUpdatedBy(), "基础信息→允差类型", "允差类型：" + instance.getTypeName(), "更新");
            }
        }
        return fetchForPage(_pageData);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "TypeId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "基础信息→允差类型", "允差类型：" + MapUtils.getString(criterion, "TypeName"), "删除");
        }
        return fetchForPage(criterion);
    }
}
